var app = getApp();
var vp = require('../../resource/js/vp.js');
Page({
  data: {
    years: Array.from({length: 50}, (_, i) => `${i+1}年`),
    drinkTypes: ['白酒', '啤酒', '红酒', '黄酒', '洋酒', '其他'],
	section:'personal',
    personalData: {
      drinking: {
        hasDrinking: 'no',
        years: '',
        yearsIndex: 0,
        type: '',
        typeIndex: 0,
        amount: '',
        hasQuit: 'no',
        quitYears: '',
        quitYearsIndex: 0
      },
      smoking: {
        hasSmoking: 'no',
        years: '',
        yearsIndex: 0,
        amount: '',
        hasQuit: 'no',
        quitYears: '',
        quitYearsIndex: 0
      }
    }
  },

  onLoad(options) {
    // 可以在这里初始化已保存的数据
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('sendData', (data) => {
		console.log('sendData',data)
      this.setData({
        personalData: data.data || this.data.personalData,
		section:data.section
      });
	  console.log('this.data.personalData',this.data.personalData)
    });
  },

  // 切换饮酒情况
  toggleDrinking(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      'personalData.drinking.hasDrinking': value
    });
  },

  // 饮酒年数变化
  changeDrinkingYears(e) {
    const index = e.detail.value;
    this.setData({
      'personalData.drinking.yearsIndex': index,
      'personalData.drinking.years': this.data.years[index]
    });
  },

  // 饮酒类型变化
  changeDrinkingType(e) {
    const index = e.detail.value;
    this.setData({
      'personalData.drinking.typeIndex': index,
      'personalData.drinking.type': this.data.drinkTypes[index]
    });
  },

  // 输入饮酒量
  inputDrinkingAmount(e) {
    this.setData({
      'personalData.drinking.amount': e.detail.value
    });
  },

  // 切换是否戒酒
  toggleQuitDrinking(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      'personalData.drinking.hasQuit': value
    });
  },

  // 戒酒年数变化
  changeQuitDrinkingYears(e) {
    const index = e.detail.value;
    this.setData({
      'personalData.drinking.quitYearsIndex': index,
      'personalData.drinking.quitYears': this.data.years[index]
    });
  },

  // 切换吸烟情况
  toggleSmoking(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      'personalData.smoking.hasSmoking': value
    });
  },

  // 吸烟年数变化
  changeSmokingYears(e) {
    const index = e.detail.value;
    this.setData({
      'personalData.smoking.yearsIndex': index,
      'personalData.smoking.years': this.data.years[index]
    });
  },

  // 输入吸烟量
  inputSmokingAmount(e) {
    this.setData({
      'personalData.smoking.amount': e.detail.value
    });
  },

  // 切换是否戒烟
  toggleQuitSmoking(e) {
    const value = e.currentTarget.dataset.value;
    this.setData({
      'personalData.smoking.hasQuit': value
    });
  },

  // 戒烟年数变化
  changeQuitSmokingYears(e) {
    const index = e.detail.value;
    this.setData({
      'personalData.smoking.quitYearsIndex': index,
      'personalData.smoking.quitYears': this.data.years[index]
    });
  },

  // 保存数据
  save() {
    // 验证必填项
    if (this.data.personalData.drinking.hasDrinking === 'yes' && !this.data.personalData.drinking.years) {
      wx.showToast({
        title: '请选择饮酒年数',
        icon: 'none'
      });
      return;
    }
    
    if (this.data.personalData.smoking.hasSmoking === 'yes' && !this.data.personalData.smoking.years) {
      wx.showToast({
        title: '请选择吸烟年数',
        icon: 'none'
      });
      return;
    }
    const requestData = {
      personalData: this.data.personalData
    };
    // 保存逻辑
    const that = this;
      // 模拟保存操作
      wx.showLoading({
        title: '保存中...',
      });
		let activeUser = wx.getStorageSync('activeUser')
		requestData.type = 2;
		
		requestData.family_id = activeUser.id
      let jsonData = JSON.stringify(requestData);
      app.util.request({
      	url: 'Familydoctor/createPersonlData',
      	data: jsonData,
      	method:'POST',
      	headers: {
      		'Content-Type': 'application/json',
      	},
      	success(res) {
      		console.log(res)
      		if(res.data.code){	
				wx.hideLoading();
				wx.showToast({
				  title: '保存成功',
				  icon: 'success'
				});
				that.setData({
				  isEditing: false
				});
				
				const eventChannel = that.getOpenerEventChannel();
				eventChannel.emit('sendData', res.data.data.content);
				setTimeout(() => {
				  wx.navigateBack();
				}, 1500);
      		}else{
      			wx.showToast({ title: '提交失败请重试' });
      		}
      	}
      })
  }
});